package generator

import (
	"context"
	"errors"
	"sync"
)

var (
	GlobalDataCenter = &DataCenter{}
)

type IdGenerator interface {
	GetId(ctx context.Context) (int64, error)
	UnmarshalId(ctx context.Context, id int64) (timeStampMs, workId, sequence int64)
}

type DataCenter struct {
	sync.Map
}

func (d *DataCenter) SetIDGenerator(key string, idGen IdGenerator) {
	d.Map.Store(key, idGen)
}

func (d *DataCenter) GetIdGenerator(key string) (IdGenerator, error) {
	value, ok := d.Map.Load(key)
	if !ok {
		return nil, errors.New("未能找到对应配置，请检查参数是否有误 : " + key)
	}
	return value.(IdGenerator), nil
}

func (d *DataCenter) DelIdGenerator(key string) {
	d.Map.Delete(key)
}
